﻿//Copyright © Omega Point 2008
//All rights reserved.
//This Software is Licensed under the New BSD License
//for a full copy of this license see http://www.codeplex.com/networkmessenger/license
//Redistributions of source code must retain the above copyright notice.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.IO;
using System.Net;
using System.Net.Sockets;
using OmegaPoint.Vista.Dwm;


namespace NetworkMessenger
{
    /// <summary>
    /// Interaction logic for SendMessageWindow.xaml
    /// </summary>
    public partial class SendMessageWindow : Window
    {
        /// <summary>
        /// Creates a new Message
        /// </summary>
        public SendMessageWindow()
        {
            InitializeComponent();
            if (Properties.Settings.Default.LastSentTo != null)
            {
                foreach (string host in Properties.Settings.Default.LastSentTo)
                {
                    toTextBox.Items.Add(host);
                }
            }
            fromTextBox.Text = Environment.UserName.ToLower() + "@" + Environment.MachineName.ToLower();
        }

        /// <summary>
        /// Creates New Message to Send To specified host
        /// </summary>
        /// <param name="toHost">Host to Send To</param>
        public SendMessageWindow(string toHost)
        {
            InitializeComponent();
            fromTextBox.Text = Environment.UserName.ToLower() + "@" + Environment.MachineName.ToLower();
            toTextBox.Text = toHost;
            toTextBox.IsReadOnly = true;
            toTextBox.IsEditable = true;
            toTextBox.IsEnabled = false;
        }

        private void SendMessage()
        {
            TcpClient cli = new TcpClient();
            //cli.Connect("localhost", 456);           
            try
            {
                System.Threading.Thread.BeginCriticalRegion();

                string toUser = toTextBox.Text.Split(new char[] { char.Parse("@") }, 2, StringSplitOptions.RemoveEmptyEntries)[0].ToLower();
                string toHost = toTextBox.Text.Split(new char[] { char.Parse("@") }, 2, StringSplitOptions.RemoveEmptyEntries)[1];
                cli = new TcpClient(toHost, Tools.GetPortFromName(toUser));
                cli.SendTimeout = 100;
                //cli.Connect("localhost", 456);                
                NetworkStream sendstream = cli.GetStream();
                byte[] buffer = ASCIIEncoding.Unicode.GetBytes(Message.Create(toUser, fromTextBox.Text, messageTextBox.Text).ToXml());
                sendstream.Write(buffer, 0, buffer.Length);
                sendstream.Flush();
                sendstream.Close();

                Tools.AddToLastRecivers(toTextBox.Text);
                Close();                
            }
            catch (Exception)
            {
                MessageBox.Show("Error Sending Message\nPlease Check To Address and Try Again", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                System.Threading.Thread.EndCriticalRegion();
                cli.Close();
            }

        }
        private void sendButton_Click(object sender, RoutedEventArgs e)
        {
            SendMessage();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (Aero.DoesOSSupportDwm() == true)
            {
                try
                {
                    Aero.ExtendGlassFrame(this, new Thickness(-1));
                }
                catch (Exception)
                {
                    //Alter Background to fill area
                    backgroundRectangle.RadiusX = 0;
                    backgroundRectangle.RadiusY = 0;
                    backgroundRectangle.Opacity = 1;
                }
            }
            else
            {
                backgroundRectangle.RadiusX = 0;
                backgroundRectangle.RadiusY = 0;
                backgroundRectangle.Opacity = 1;
            }
        }
    }
}
